package cn.itcast.web.task;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

public class ContractOverdueTask {

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;

     public void contractOverdueTask(){
         ContractExample contractExample = new ContractExample();
         //拿到所有的购销合同
         List<Contract> contractList = contractDao.selectByExample(contractExample);
         //提醒用户的交货过期时间提前三天开始发消息
         if (contractList!=null) {
             for (Contract contract : contractList) {
                 //拿到交货期限
                 Date deliveryPeriod = contract.getDeliveryPeriod();
                 //转换成毫秒值
                 long outTime = deliveryPeriod.getTime();
                 //拿到现在的时间
                 Date date = new Date();
                 long nowDate = date.getTime();
                 boolean flag = ((outTime-nowDate)/(1000*24*60*60)<=3) && ((outTime-nowDate)/(1000*24*60*60)>=0);
                 if (flag){
                     //拿到购销合同的签单人
                     String inputBy = contract.getInputBy();
                         User user = userDao.findByUserName(inputBy);
                         if (user!=null) {
                             rabbitTemplate.convertAndSend("email-exchange", "outTimeUser.dateOute", user);
                         }
                 }

             }
         }





     }


}
